// @ts-ignore
import {debounce} from 'uview-plus';
// import {useStore} from '@/store/user';

interface NavigateOptions {
    url: string;
    type?: 'navigateTo' | 'redirectTo' | 'reLaunch' | 'switchTab' | 'navigateBack';
    events?: Record<string, any>;
    params?: Record<string, any>;
}

interface NavigationMethods {
    [key: string]: (options: any) => void;
}

const methods = {
    goPage(options: NavigateOptions, params: Record<string, any> = {}) {
        const {url, type = 'navigateTo',  events = {}} = options;
        if (!url) {
            uni.showToast({
                title: '路由错误请重试！',
                icon: 'none',
            }).then(() =>{});
            // this.$u.toast('路由错误请重试！')
            return;
        }

        let queryString = '';
        if (params && typeof params === 'object') {
            queryString = '?' + Object.keys(params).map((key) => `${key}=${params[key]}`).join('&');
        }

        uni.showLoading({
            title: '加载中',
        }).then(() => {});

        const navMethods: NavigationMethods = {
            navigateTo: (options) => uni.navigateTo(options),
            redirectTo: (options) => uni.redirectTo(options),
            reLaunch: (options) => uni.reLaunch(options),
            switchTab: (options) => uni.switchTab(options),
            navigateBack: (options) => uni.navigateBack(options),
        };

        debounce(() => {
            navMethods[type]({
                url: url + queryString,
                events: events,
                success: () => {
                    uni.hideLoading();
                },
                fail: () => {
                },
                complete: () => {
                    // console.log(result);
                },
            });
        }, 500);
    },

    pageBack(wait:number = 0,delta:number=1) {
        setTimeout(() => {
            uni.navigateBack({
                delta: 1,
            }).then(() => {});
        }, wait);
    },

    copyTxt(value: string) {
        uni.setClipboardData({
            data: value,
            success: () => {
                uni.showToast({
                    title: '已复制到剪切板',
                    icon: 'success',
                }).then(() => {});
            },
        });
    },

};

export default methods;